package org.okcjug.december.google;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

import org.junit.Test;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;

public class ImmutableCollectionsStuffolla {
	private static final String BRIAN = "brian@elsewhere.com";

	private static final String PAUL = "paul@somewhere.com";

	//old way
	public static final Set<String> EMAIL_ADDRESS_OLD_WAY
		= Collections.unmodifiableSet(
				new LinkedHashSet<String>(
						Arrays.asList(PAUL, BRIAN)));
	
	//new constant set
	public static final ImmutableSet<String> THE_EMAILS =
		ImmutableSet.of(PAUL, BRIAN);
	
	@Test
	public void immutable(){
		try{
			THE_EMAILS.add("can i?");
			fail("Shouldn't happen");
		}catch(UnsupportedOperationException e){
			//should happen
		}
		
		try{
			THE_EMAILS.remove(PAUL);
			fail("Shouldn't happen");
		}catch(UnsupportedOperationException e){
			//should happen
		}
	}
	
	@Test
	public void immutableToo(){
		Set<String> emails = Sets.newLinkedHashSet();
		emails.add(PAUL);
		emails.add(BRIAN);
		
		ImmutableSet<String> immutable = ImmutableSet.copyOf(emails);
		try{
			immutable.add("can i?");
			fail("Shouldn't happen");
		}catch(UnsupportedOperationException e){
			//should happen
		}
		
		try{
			immutable.remove(PAUL);
			fail("Shouldn't happen");
		}catch(UnsupportedOperationException e){
			//should happen
		}
 	}
}
